import Menu from "./Menu";
import SoundsManager from "../soundmanager";
import AnimatedNode from "../amn/AnimatedNode";
import GameConst from "../GameConst";
import Utils from "../utils";

export default class MainMenu extends Menu{
    constructor() {
       super();
        var n = this;

        this.heroPositions = [[129, 983, 0], [665, 698, -44], [659, 239, -119], [165, -16, -180], [-17, 389, 118], [-17, 735, 69]];
        this.blinks = [];
        this.blinksDelays = [];
        this.prevHeroId = -1;
        this.crossButtons = [];
        this.playCrossIn = 0;
        this.blinkData = [[392.6, 441.1, 1, 1], [422.4, 369.55, 1.1253662109375, 1.1253662109375], [480.35, 505.1, 1.0650634765625, 1.0650634765625], [496.1, 545.5, 1.0650634765625, 1.0650634765625], [372, 543.9, 1.499755859375, 1.499755859375], [339.5, 505.7, 1.002532958984375, 1.002532958984375], [282.35, 531.05, 1.3023529052734375, 1.3023529052734375], [584.75, 547.8, 1.3023529052734375, 1.3023529052734375], [524.35, 522.6, .9369049072265625, .9369049072265625], [440.6, 477.95, .9369049072265625, .9369049072265625], [376.25, 596.65, 1.0650634765625, 1.0650634765625], [439.35, 636.85, 1.0650634765625, 1.0650634765625], [435.65, 593.3, .72271728515625, .72271728515625], [198.05, 605.65, 1.0650634765625, 1.0650634765625], [274, 416, 1.0650634765625, 1.0650634765625], [588.45, 478, .7136383056640625, .7136383056640625], [459.05, -135.35, 1, 1]];
        var r = new createjs.Container;
        this.sprite = r;
        this.back = Utils.createBitmap("main menu");
        r.addChild(this.back);
        var i =GameConst.episode <= 4 ? this.blinkData.length : 0;
        for (var s = 0; s < i; ++s) {
            var o = this.blinkData[s];
            var u = new AnimatedNode(GameConst.animationManager.getAnimation("gloss anim"), 1 / 30, null);
            r.addChild(u);
            u.mouseChildren = u.mouseEnabled = false;
            u.x = o[0];
            u.scaleX = o[2];
            u.scaleY = o[3];
            this.blinks.push(u);
            u.visible = false;
            u.stop();
            u.addAction(u.totalFrames - 1, 0, -1);
            this.blinksDelays.push(this.getBlinkDelay(true));
        }
        var a = needCross ? t.episodeCross[App.episode - 1].length : 0;
        for (var s = 0; s < a; ++s) {
            var f = createBitmap("cross_link" + (s + 1));
            if (f) {
                r.addChild(f);
                f.pivotX = 91;
                f.pivotY = 70;
            }
            this.crossButtons.push(f);
        }
        this.logo = new AnimatedNode(App.game.animationManager.getAnimation("logo"), 1 / 30, null);
        if (App.episode <= 2) {
            var l = createSpriteFromSpritesheet("logo");
            l.x = 30;
            l.y = 65;
            this.logo.getPart(0).addChild(l);
        }
        this.logo.scaleX = this.logo.scaleY = .85;
        r.addChild(this.char);
        this.button = new AnimatedNode(App.game.animationManager.getAnimation("button"), 1 / 30, null);
        r.addChild(this.button);
        this.button.addAction(97, 0, 0);
        this.button.addAction(106, 0, -1);
        this.button.addAction(this.button.totalFrames - 1, 1, 0);
        var c = new ClickableObject(this.button);
        c.setCircle(120, 120, 120);
        c.callback = function () {
            n.onPlayDown();
        };
        this.clickables.push(c);
        var h = new SoundButton(true, r, 570, 115 - 50);
        this.animatedButtons.push(h);
        this.clickables.push(h);
        var h = new SoundButton(false, r, 570, 215 - 50);
        this.animatedButtons.push(h);
        this.clickables.push(h);
        
        this.animatedButtons.push(h);
        this.clickables.push(h);
        r.addChild(this.logo);
        this.brandLogo = new LogoObject(r,GameConst.SCREEN_W / 2, 0, 1);
        this.clickables.push(this.brandLogo);
        this.moreGames = new MoreGamesButton(r,GameConst.SCREEN_W / 2, 0, 1);
        this.clickables.push(this.moreGames);
        if (this.crossButtons) {
            for (var s = 0; s < this.crossButtons.length; ++s) {
                var c = new ClickableObject(this.crossButtons[s]);
                c.setRect(0, 0, 180, 140);
                if (s == 0)
                    c.callback = function () {
                        n.onCrossButtonDown1();
                    };
                else if (s == 1)
                    c.callback = function () {
                        n.onCrossButtonDown2();
                    };
                else if (s == 2)
                    c.callback = function () {
                        n.onCrossButtonDown3();
                    };
                this.clickables.push(c);
            }
        }
       Utils.removeClip(this.char);
        r.addChild(this.char);
        this.onResize();
    }
}
var t = MainMenu;
    t.getCrossTokenById = function (e) {
        return t.crossLinks[t.episodeCross[App.episode - 1][e]];
    };
    t.prototype.onCrossButtonDown1 = function () {
        this.onCrossButtonDown(0);
    };
    t.prototype.onCrossButtonDown2 = function () {
        this.onCrossButtonDown(1);
    };
    t.prototype.onCrossButtonDown3 = function () {
        this.onCrossButtonDown(2);
    };
    t.prototype.onCrossButtonDown = function (e) {
        if (this.crossButtons) {
            var n = this.crossButtons[e];
            createjs.Tween.get(n, {
                loop: false
            }).wait(0).to({
                scaleX: 1.15,
                scaleY: 1.15
            }, 150, createjs.Ease.quadOut).to({
                scaleX: 1,
                scaleY: 1
            }, 150, createjs.Ease.quadIn);
            var r = t.getCrossTokenById(e);
            try {
                apiInstance.Branding.getLink(r).action();
            } catch (i) {}

        }
    };

    t.prototype.getCharY = function (e) {
        if (e < 650)
            e = e + (App.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2;
        else
            e = e - 978 +GameConst.ACTUAL_H + (App.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2;
        return e;
    };
    t.prototype.getHeroPosData = function (e) {
        var t = this.prevHeroId;
        if (e)
            t = 0;
        else {
            while (t == this.prevHeroId)
                t =Utils.getInt(this.heroPositions.length);
        }
        this.prevHeroId = t;
        var n = this.heroPositions[t];
        this.lastHeroData = n;
        return n;
    };
    t.prototype.getBlinkDelay = function (e) {
        if (typeof e === "undefined") {
            e = false;
        }
        returnUtils.lerp(e ? 0 : 6, 16, Math.random());
    };
    t.prototype.onPlayDown = function () {
        try {
            this.button.gotoAndPlay(99);
            MenuManager.instance.show(MenuManager.instance.map);
            SoundsManager.instance.playSound("play_button");
        } catch (e) {
            alert("play down " + e);
        }
    };
    t.prototype.onResize = function () {
        e.prototype.onResize.call(this);
        this.logo.x = 45;
        this.logo.y = 0 + (App.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2 + (App.episode > 2 ? -5 : 20);
        this.button.x = 401;
        this.button.y =GameConst.ACTUAL_H - 235 + (App.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2;
        var t =GameConst.ACTUAL_H -GameConst.FULL_SCREEN_H;
        this.back.y = Math.max(this.logo.y - 50, t);
        var n = this.blinks.length;
        for (var r = 0; r < n; ++r) {
            var i = this.blinks[r];
            i.y = this.blinkData[r][1];
        }
        for (var r = 1; r <= 3; ++r) {
            this.clickables[r].sprite.x = 525 + (r - 2) * 75;
            this.clickables[r].sprite.y = 40 + (App.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2 + (r == 3 ? 0 : 0);
            this.clickables[r].sprite.scaleX = this.clickables[r].sprite.scaleY = r == 3 ? .75 : .75;
            this.clickables[r].updateRectScale();
        }
        if (this.brandLogo.sprite)
            this.brandLogo.sprite.y =GameConst.ACTUAL_H - 30 + (App.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2;
        if (this.moreGames.sprite) {
            this.moreGames.sprite.x = this.button.x + 115 - 390;
            this.moreGames.sprite.y = this.button.y - 50 - 20;
        }
        if (this.crossButtons) {
            var s = [[340,GameConst.ACTUAL_H + (App.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2 - 140], [390,GameConst.ACTUAL_H + (App.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2 - 270], [540,GameConst.ACTUAL_H + (App.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2 - 320]];
            var o = this.crossButtons.length == 2 ? 1 : 0;
            for (var r = 0; r < this.crossButtons.length; ++r) {
                this.crossButtons[r].x = s[r + o][0];
                this.crossButtons[r].y = s[r + o][1];
            }
        }
        if (this.char && this.lastHeroData) {
            var u = this.lastHeroData;
            this.char.x = u[0];
            this.char.rotation = u[2];
            this.char.y = this.getCharY(u[1]);
        }
    };
    t.prototype.show = function () {
        e.prototype.show.call(this);
        this.onResize();
        this.button.gotoAndPlay(0);
        this.button.visible = true;
        this.button.alpha = 1;
        this.setPlayButtonTime(true);
        this.playCrossIn = 1;
    };
    t.prototype.hide = function () {
        e.prototype.hide.call(this);
    };
    t.prototype.setPlayButtonTime = function (e) {
        if (typeof e === "undefined") {
            e = false;
        }
        if (this.button.currentFrame != 106)
            this.button.gotoAndPlay(e ? 130 : 0);
        this.playButtonIn =Utils.lerp(5, 12, Math.random());
    };
    t.prototype.setCrossTime = function () {
        this.playCrossIn =Utils.lerp(3, 9, Math.random());
        var e = this.crossButtons[Utils.getInt(this.crossButtons.length)];
        var t = e.y;
        var n = 6;
        createjs.Tween.get(e, {
            loop: false
        }).to({
            y: t + n
        }, 100).to({
            y: t
        }, 100).to({
            y: t + n
        }, 100).to({
            y: t
        }, 100).to({
            y: t + n
        }, 100).to({
            y: t
        }, 100);
    };
    t.prototype.update = function (t) {
        e.prototype.update.call(this, t);
        this.logo.update(t);
        this.button.update(t);
        this.playButtonIn -= t;
        if (this.playButtonIn <= 0 && !this.button.isPlaying)
            this.setPlayButtonTime();
        var n = this.blinks.length;
        for (var r = 0; r < n; ++r) {
            var i = this.blinks[r];
            if (!i.isPlaying) {
                this.blinksDelays[r] -= t;
                if (this.blinksDelays[r] <= 0) {
                    this.blinksDelays[r] = this.getBlinkDelay();
                    i.gotoAndPlay(0);
                }
            } else
                i.update(t);
            i.visible = i.isPlaying;
        }
        this.playCrossIn -= t;
        if (this.playCrossIn < 0 && needCross && this.crossButtons.length > 0)
            this.setCrossTime();
    };
    t.episodeCross = [[1, 2], [0, 2], [0, 1], [0, 1, 2]];
    t.crossLinks = ["cross_promo_BTC_episode_1", "cross_promo_BTC_episode_2", "cross_promo_BTC_episode_3", "cross_promo_BTC_episode_4"];
